package com.easytake.emotioncat.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class StringUtil {

    public static String getUrlParamString(Object paramObj, String... extendProps) {
        // todo do with extendProps
        try {
            Field[] declaredFields = paramObj.getClass().getDeclaredFields();
            List<String> paramPatterns = new ArrayList<>();
            for (Field field : declaredFields) {
                String name = field.getName();
                field.setAccessible(true);
                Object val = field.get(paramObj);
                paramPatterns.add(name + "=" + val.toString());
            }
            if (CollectionUtils.isEmpty(paramPatterns)) {
                return "";
            }
            return "?" + String.join("&", paramPatterns.toArray(new String[]{}));
        } catch (Exception e) {
            log.error("getUrlParamString error", e);
        }
        return "";
    }

}
